import request from '@/api/request.js';

const data = {
    state: {
        // 产品分类数据
        isLoadingCategory: false,
        needLoadCategory: true,
        categories: []
    },
    mutations: {
        setLoadCategoryStatus(state, status) {
            state.needLoadCategory = status;
        },
        setCategories(state, data) {
            state.needLoadCategory = false;
            state.categories = data;
        }
    },
    actions: {
        getCategories(context) {
            if(!context.state.needLoadCategory) return false;
            if(context.state.isLoadingCategory) return false;
            context.state.isLoadingCategory = true;
            request.get('/project/category/index', {params: {pageSize: 100}}).then(res => {
                context.state.isLoadingCategory = false;
                context.commit('setCategories', res.items || []);
            }).catch(() => {
                context.state.isLoadingCategory = false;
            })
        }
    }
};

export default data;
